import configReader


def parseFlightResults(key, FlightResultsList):
    resultIds = FlightResultsList['resultIds']
    results = FlightResultsList['results']
    airlineKey = key.split('/')[2]

    datas = []
    limit = int(configReader.getConfig('limit'))
    if len(resultIds) > limit:
        resultIds = resultIds[:limit]

    for rindex, rId in enumerate(resultIds):
        result = results[rId]
        airlineName = ''
        legInfos = []
        if 'legs' in result:
            legs = result['legs']
            for leg in legs:
                if 'segments' in leg:
                    segments = leg['segments']
                    for segment in segments:
                        airline = segment['airline']
                        airlineName = airline['name']
                        departure = segment['departure']
                        departureTime = departure['isoDateTimeLocal']
                        departureAirport = departure['airport']
                        departureName = departureAirport['displayName']
                        arrival = segment['arrival']
                        arrivalTime = arrival['isoDateTimeLocal']
                        arrivalAirport = arrival['airport']
                        arrivalName = arrivalAirport['displayName']
                        legInfo = {'departureName': departureName,
                                   'departureTime': departureTime,
                                   'arrivalName': arrivalName,
                                   'arrivalTime': arrivalTime}
                        legInfos.append(legInfo)

        if 'optionsByFare' in result:
            optionsByFare = result['optionsByFare']
            item = optionsByFare[0]
            options = item['options']
            min_price = getMinPrice(options)
            for i, option in enumerate(options):
                providerInfo = option['providerInfo']
                displayName = providerInfo['displayName']
                fees = option['fees']
                rawPrice = fees['rawPrice']
                # print(str(displayName) + '   |    ' + str(rawPrice))
                airInfo = dict()
                datas.append(airInfo)
                airInfo['航线'] = airlineKey
                airInfo['结果序号'] = rindex
                airInfo['航司名称'] = airlineName
                airInfo['供应商名'] = displayName
                airInfo['供应商价格'] = rawPrice
                if displayName == 'iWofly':
                    airInfo['供应商排名'] = i + 1
                    airInfo['最低价差值'] = str(int(rawPrice) - int(min_price))
                else:
                    airInfo['供应商排名'] = '-'
                    airInfo['最低价差值'] = '-'

                for index, legInfo in enumerate(legInfos):
                    airInfo[str('出发机场' + str(index))] = legInfo['departureName']
                    airInfo['到达机场' + str(index)] = legInfo['arrivalName']
                    airInfo['出发时间' + str(index)] = legInfo['departureTime']
                    airInfo['到达时间' + str(index)] = legInfo['arrivalTime']

    print('----------- 查询【' + airlineKey + '】数据, ' + '查询到[' + str(len(datas)) + ']条数据' + '----------- ')
    return datas


def getMinPrice(options):
    if len(options) == 0:
        return 0
    min_price = min(options, key=lambda x: x['fees']['rawPrice'])
    return min_price['fees']['rawPrice']
